﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.BusinessFacades;
using com.eforceglobal.crux.views.Enums;
using com.eforceglobal.crux.views.Administration;
using com.eforceglobal.crux.logging;
using com.eforceglobal.crux.logging.Enums;
using com.eforceglobal.crux.exceptions;

namespace com.eforceglobal.crux.controllers.Administration
{
    public class ManageSubLocationsController
    {
        IManageSubLocations _view = null;
        public ManageSubLocationsController(IManageSubLocations view)
        {
            _view = view;
        }

        public void InitView(bool IsPostBack)
        {
            if (!IsPostBack)
                SwitchToListMode();
        }

        public void OpenSubLocation(int SubLocationID)
        {
            SubLocationFacade subLocationFacade = new SubLocationFacade();
            SubLocationInformation subLocationInformation = new SubLocationInformation();
            subLocationInformation = subLocationFacade.GetSubLocation(SubLocationID);
            _view.CurrentSubLocation = subLocationInformation; 
            _view.FormMode = FormMode.AddEdit;
        }

        public void CreateSubLocation()
        {
            OpenSubLocation(0);
        }

        public void SaveSubLocation()
        {
            try
            {
                SubLocationFacade subLocationFacade = new SubLocationFacade();
                subLocationFacade.SaveSubLocation(_view.CurrentSubLocation);
                SwitchToListMode();
            }
            catch (CruxDatabaseException ex)
            {
                ApplicationLogger.LogApplicationMessage(
                            ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager
                            .GetApplicationError(ex.ErrorNameOrCode, ex.Message);
            }
            catch (Exception ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("101", ex.Message);
            }
            
        }

        private void SwitchToListMode()
        {
            _view.ErrorDetails = null;
            _view.CurrentSubLocation = new SubLocationInformation();
            SubLocationFacade subLocationFacade = new SubLocationFacade();
            _view.SubLocations = subLocationFacade.GetSubLocations(_view.LocationID);
            _view.FormMode = FormMode.List;
        }

        public void CancelAddEdit()
        {
            SwitchToListMode();
        }

        public void DeleteSubLocation(int SubLocationID)
        {
            
            try
            {
                SubLocationFacade subLocationFacade = new SubLocationFacade();
                subLocationFacade.DeleteSubLocation(SubLocationID);
                SwitchToListMode();
            }
            catch (Exception ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("101", ex.Message);
            }
        }


    }
}
